package com.yspa.yspa.util;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import net.sf.json.JSONObject;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.baidu.yun.push.model.PushMsgToAllRequest;
import com.baidu.yun.push.model.PushMsgToTagRequest;
import com.hsnn.medstgmini.util.ParseDate;
import com.yspa.util.PushMsg;
import com.yspa.yspa.service.YspaInterfaceManager;

@Component
@Scope("singleton")
public class PushContentTask{
private static YspaInterfaceManager yspaInterfaceManager;
public static void getContent (){
		Timer timer = new Timer();  
        timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				try{
					String lastTime = yspaInterfaceManager.getLastWorkTime();
					String nowTime = ParseDate.parseFullFormat(new Date());
					
					// 通知公告推送
					List<Map<String, Object>> activitylist = yspaInterfaceManager.getHactivityList(lastTime, nowTime);
					for(Map<String, Object> activityMap : activitylist){
						// 创建 Android的通知
						JSONObject notification = new JSONObject();
						notification.put("title", "通知公告");
						notification.put("description", activityMap.get("acttitle"));
	
						PushMsgToAllRequest request = new PushMsgToAllRequest()
								.addMsgExpires(new Integer(3600*24)) //设置消息的有效时间,单位秒,默认3600*24.
								.addMessageType(1)	//设置消息类型,0表示透传消息,1表示通知,默认为0.
								.addMessage(notification.toString()) // 添加透传消息
								.addDeviceType(3);//设置设备类型，deviceType => 1 for web, 2 for pc, 3 for android, 4 for ios, 5 for wp.
						PushMsg.pushMsgToAll(request);
					}
					
					// 用药提醒推送
					List<Map<String, Object>> hpmedicinesuglist = yspaInterfaceManager.getPushHPMedicineSugList(lastTime, nowTime);
					for (Map<String, Object> hpmedicinesugMap : hpmedicinesuglist) {
						PushMsgToTagRequest request = new PushMsgToTagRequest()
								.addTagName("" + hpmedicinesugMap.get("userno"))// 渠道ID,userno
								.addMsgExpires(new Integer(3600 * 24))
								.addMessageType(1)// 添加透传消息
								.addMessage(
										"{\"title\":\"用药提醒\",\"description\":\""
												+ hpmedicinesugMap.get("medicine_name")
												+ "(" + hpmedicinesugMap.get("medicine_time") + ")" + "\"}").addDeviceType(3);
	
						PushMsg.pushMsgToTag(request);
					}
					
					// 叮咛提醒
					List<Map<String, Object>> calendarlist = yspaInterfaceManager.getPushCalendarlogList(lastTime, nowTime);
					for (Map<String, Object> calendarMap : calendarlist) {
						PushMsgToTagRequest request = new PushMsgToTagRequest()
								.addTagName("" + calendarMap.get("userno"))// 渠道ID,userno
								.addMsgExpires(new Integer(3600 * 24))
								.addMessageType(1)// 添加透传消息
								.addMessage(
										"{\"title\":\"健康叮咛\",\"description\":\""
												+ calendarMap.get("descr") + "\"}").addDeviceType(3);
	
						PushMsg.pushMsgToTag(request);
					}
					
					// 量测排程
					List<Map<String, Object>> measemplist = yspaInterfaceManager.getMeasempList(lastTime, nowTime);
					for (Map<String, Object> measempMap : measemplist) {
						PushMsgToTagRequest request = new PushMsgToTagRequest()
								.addTagName("" + measempMap.get("userno"))// 渠道ID,userno
								.addMsgExpires(new Integer(3600 * 24))
								.addMessageType(1)// 添加透传消息
								.addMessage(
										"{\"title\":\"量测排程\",\"description\":\""
												+ measempMap.get("measure_name")
												+ "(" + measempMap.get("keeptime") + ")" + "\"}").addDeviceType(3);
	
						PushMsg.pushMsgToTag(request);
					}
					
					// 饮食排程
//					List<Map<String, Object>> foodemplist = yspaInterfaceManager.getFoodempList(lastTime, nowTime);
					
					// 运动排程
					List<Map<String, Object>> exeremplist = yspaInterfaceManager.getExerempList(lastTime, nowTime);
					for (Map<String, Object> exerempMap : exeremplist) {
						PushMsgToTagRequest request = new PushMsgToTagRequest()
								.addTagName("" + exerempMap.get("userno"))// 渠道ID,userno
								.addMsgExpires(new Integer(3600 * 24))
								.addMessageType(1)// 添加透传消息
								.addMessage(
										"{\"title\":\"运动排程\",\"description\":\""
												+ exerempMap.get("exername")
												+ "(" + exerempMap.get("times") + "分钟)" + "\"}").addDeviceType(3);
	
						PushMsg.pushMsgToTag(request);
					}
					
					// 预约行程
					List<Map<String, Object>> hPProEventlist = yspaInterfaceManager.getHPProEventList(lastTime, nowTime);
					for (Map<String, Object> hPProEventMap : hPProEventlist) {
						PushMsgToTagRequest request = new PushMsgToTagRequest()
								.addTagName("" + hPProEventMap.get("userno"))// 渠道ID,userno
								.addMsgExpires(new Integer(3600 * 24))
								.addMessageType(1)// 添加透传消息
								.addMessage(
										"{\"title\":\"预约行程\",\"description\":\""
												+ hPProEventMap.get("event_subject") + "\"}").addDeviceType(3);
	
						PushMsg.pushMsgToTag(request);
					}
					
					yspaInterfaceManager.updateWorkTime(nowTime);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}, 100000, 300000); 
	}
public static YspaInterfaceManager getYspaInterfaceManager() {
	return yspaInterfaceManager;
}
public static void setYspaInterfaceManager(YspaInterfaceManager yspaInterfaceManager) {
	PushContentTask.yspaInterfaceManager = yspaInterfaceManager;
}
}

